package swu.cx.glidetest

import android.Manifest
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //申请权限
        applyForFileReadJurisdction()
       // 1.加载网络图片
        Glide.with(this)
            .load("http://192.168.0.103/iamge4.jpg")
            .apply(
                RequestOptions.bitmapTransform(
                    RoundedCornersTransformation(
                        500,
                        0,
                        RoundedCornersTransformation.CornerType.ALL
                    )
                )
            )
            .into(glide_image1)
        // 2.加载Drawable资源图片
        Glide.with(this)
            .load(R.drawable.image1)
            .into(glide_image2)
        // 3.加载手机SD卡图片
        val imgPath = Environment.getExternalStorageDirectory().path+"/a/retrofitDownload.jpg"
        val file = File(imgPath)
        Glide.with(this)
            .load(Uri.fromFile(file))
            .apply(RequestOptions.bitmapTransform(BlurTransformation(100, 1)))
            .into(glide_image3)
        // 4.加载网络gif
        Glide.with(this)
            .load("http://192.168.0.103/internet.gif")
            .placeholder(R.drawable.ic_launcher_foreground)
            .into(glide_image4)
        // 5.加载Drawable资源gif图片
        Glide.with(this)
            .load(R.drawable.drawable)
            .into(glide_image5)
        // 6.加载手机SD卡gif图片
        val gifPath = Environment.getExternalStorageDirectory().path+"/a/SD卡.gif"
        Glide.with(this)
            .load(gifPath)
            .into(glide_image6)
        val multi = MultiTransformation(
            BlurTransformation(25),
            RoundedCornersTransformation(500, 0, RoundedCornersTransformation.CornerType.ALL)
        )
        // 7.加载本地视频
        val videoPath = Environment.getExternalStorageDirectory().path+"/a/Test.mp4"
        val videoFile = File(videoPath)
        Glide.with(this)
            .load(videoFile)
            .apply(RequestOptions.bitmapTransform(multi))
            .into(glide_image7)

        // 8.设置缩略图比例，然后先加载，再加载原图
        val localThumbnail = Environment.getExternalStorageDirectory().path+"/a/本地缩略图.png"
        Glide.with(this)
            .load(localThumbnail)
            .thumbnail(0.1f)
            .centerCrop()
            .into(glide_image8)

        // 9.先创建一个缩略图对象，然后，先加载缩略图，再加载原图
        val thumbnailRequest = Glide.with(this).load("http://192.168.0.103/girl1.jpg")
        Glide.with(this)
            .load("http://192.168.0.103/girl3.jpg")
            .thumbnail(thumbnailRequest)
            .centerCrop()
            .into(glide_image9)

    }
    fun applyForFileReadJurisdction(){
        if (Build.VERSION.SDK_INT >= 23) {
            val REQUEST_CODE_CONTACT = 101
            val permissions = arrayOf<String>(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            //验证是否许可权限
            for (str in permissions) {
                if (this.checkSelfPermission(str) !== PackageManager.PERMISSION_GRANTED) {
                    //申请权限
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT)
                    return
                }
            }
        }
    }
}
